#include "projectwizard.h"
#include <QtGui>

ProjectWizard::ProjectWizard(QWidget *parent) :
  QWizard(parent)
{
  addPage(new ProjectInfoPage);
}

void ProjectWizard::accept() {
  QString path = field("path").toString();
  QString name = field("name").toString();
  QString app_path = QString("src/").append(name);
  QString app_init, app_type;
  int type = field("type").toInt();
  if(type == 0) {
    app_type = "GAE";
    app_init = QString(app_path).append("/init.go");
  } else if(type == 1) {
    app_type = "NAV";
    app_init = QString(app_path).append("/main.go");
  } else {
    app_type = "NAV";
    app_init = QString(app_path).append("/main.go");
  }
  QDir dir(path);
  if(dir.exists()) {
    QMessageBox::warning(this, "Cannot creat Project",
                         QString("Folder %1 already exists.").arg(path));
    QWizard::accept();
    return;
  }
  if(!dir.mkpath(".") || !dir.mkpath(app_path)
     || !dir.mkdir("pkg") || !dir.mkdir("bin")) {
    QMessageBox::warning(this, "Cannot creat Project",
                         QString("Cannot creat new project.").arg(path));
    QWizard::accept();
    return;
  }
  bool yaml_ok = false;
  QFile yaml(dir.filePath("src/app.yaml"));
  if(yaml.open(QIODevice::ReadWrite)) {
    yaml.write(QString("application: %1\nversion: 1\nruntime: go\napi_version: go1\n\nhandlers:\n- url: /.*\n  script: _go_app")
               .arg(name).toUtf8());
    yaml_ok = true;
  }
  bool go_ok = false;
  QFile go(dir.filePath(app_init));
  if(go.open(QIODevice::ReadWrite)) {
    go.write(QString("package %1\n\nimport (\n	\"fmt\"\n	\"net/http\"\n)\n\nfunc handle(w http.ResponseWriter, r *http.Request) {\n	fmt.Fprint(w, \"\")\n}\n\nfunc init() {\n	http.HandleFunc(\"/\", handle)\n}")
             .arg(name).toUtf8());
    go_ok = true;
  }

  QFile ndepro(dir.filePath(QString(name).append(".ndepro")));
  if(ndepro.open(QIODevice::ReadWrite)) {
    QString data;
    QXmlStreamWriter xml(&data);
    xml.setAutoFormatting(true);
    //open project
    xml.writeStartElement("Project");
    xml.writeAttribute("Name", name);
    xml.writeAttribute("Type", app_type);
    //open src tag
    xml.writeStartElement("Src");
    xml.writeAttribute("Path", "src");
    if(yaml_ok) {
      //open yaml tag
      xml.writeStartElement("Yaml");
      xml.writeAttribute("Path", "src/app.yaml");
      //close yaml tag
      xml.writeEndElement();
    }
    //open codes tag
    xml.writeStartElement("Codes");
    xml.writeAttribute("Path", app_path);
    if(go_ok) {
      //open go tag
      xml.writeStartElement("Go");
      xml.writeAttribute("Path", app_init);
      //close go tag
      xml.writeEndElement();
    }
    //end codes tag
    xml.writeEndElement();
    //end src tag
    xml.writeEndElement();
    //open pkg tag
    xml.writeStartElement("Pkg");
    xml.writeAttribute("Path", "pkg");
    //end pkg tag
    xml.writeEndElement();
    //open pkg tag
    xml.writeStartElement("Bin");
    xml.writeAttribute("Path", "bin");
    //end pkg tag
    xml.writeEndElement();
    //close project
    xml.writeEndElement();
    ndepro.write(data.toUtf8());
  }
  ndepro.close();
  emit ProjectCreated(ndepro.fileName());
  QWizard::accept();
}

ProjectInfoPage::ProjectInfoPage(QWidget *parent) :
  QWizardPage(parent)
{
  setTitle("Create new Project");
  QFormLayout* layout = new QFormLayout;
  setLayout(layout);

  QComboBox* type = new QComboBox;
  registerField("type", type);
  type->addItem("Google App Engine");
  type->addItem("Navtive App");
  layout->addRow("Type", type);

  QLineEdit* name = new QLineEdit;
  registerField("name*", name);
  layout->addRow("Name", name);

  QHBoxLayout* path_layout = new QHBoxLayout;
  layout->addRow("Location", path_layout);

  QLineEdit* path = new QLineEdit;
  registerField("path", path);
  path_layout->addWidget(path);
  QPushButton* path_bt = new QPushButton("Browse...");
  path_layout->addWidget(path_bt);

  QFileDialog* path_browse = new QFileDialog(this);
  path_browse->setFileMode(QFileDialog::Directory);
  connect(path_bt, SIGNAL(clicked()), path_browse, SLOT(exec()));
  connect(path_browse, SIGNAL(fileSelected(QString)), this,
          SLOT(SetProjectDir(QString)));
}

void ProjectInfoPage::SetProjectDir(QString path) {
  setField("path", path.append("/").append(field("name").toString()));
}
